package com.appspot.myjsf2;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import oauth.signpost.signature.SignatureMethod;

public class OAuther {

	public static OAuthConsumer consumer = new DefaultOAuthConsumer(
		      "myjsf2.appspot.com", "hDl5q4PRjDrzxFWokWI0B0Wx",
		      SignatureMethod.HMAC_SHA1);
    //public static String scope = "http://www.blogger.com/feeds";
    public static String scope = "http://www.google.com/m8/feeds/";
    public static OAuthProvider provider = null;
    static{
    	try {
			 provider = new DefaultOAuthProvider(consumer,
   
			    "https://www.google.com/accounts/OAuthGetRequestToken?scope="
			            + URLEncoder.encode(scope, "utf-8"),
			    "https://www.google.com/accounts/OAuthGetAccessToken",
			    "https://www.google.com/accounts/OAuthAuthorizeToken?hd=default");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public static String getAuthUrl(String authUrl) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException{
//    	return provider.retrieveRequestToken(OAuth.OUT_OF_BAND);
    	return provider.retrieveRequestToken(authUrl);
    }
    
    public static void main(String[] args) throws Exception {

//      OAuthConsumer consumer = new DefaultOAuthConsumer(
//      "matthiaskaeppler.de", "etpfOSfQ4e9xnfgOJETy4D56",
//               SignatureMethod.HMAC_SHA1);

OAuthConsumer consumer = new DefaultOAuthConsumer(
      "myjsf2.appspot.com", "hDl5q4PRjDrzxFWokWI0B0Wx",
      SignatureMethod.HMAC_SHA1);

        String scope = "http://www.blogger.com/feeds";
        OAuthProvider provider = new DefaultOAuthProvider(consumer,
                "https://www.google.com/accounts/OAuthGetRequestToken?scope="
                        + URLEncoder.encode(scope, "utf-8"),
                "https://www.google.com/accounts/OAuthGetAccessToken",
                "https://www.google.com/accounts/OAuthAuthorizeToken?hd=default");

        System.out.println("Fetching request token...");

        String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

        System.out.println("Request token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());
        
        System.out.println("Now visit:\n" + authUrl
                + "\n... and grant this app authorization");
        System.out.println("Enter the verification code and hit ENTER when you're done:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String verificationCode = br.readLine();

        System.out.println("Fetching access token...");
        
        provider.retrieveAccessToken(verificationCode.trim());

        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        URL url = new URL("http://www.blogger.com/feeds/default/blogs");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage());
    }
    
    public static String getData(String verificationCode) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException, IOException{
        provider.retrieveAccessToken(verificationCode.trim());

        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

//        URL url = new URL("http://www.blogger.com/feeds/default/blogs");
        //URL url = new URL("http://www.blogger.com/feeds/default/blogs");
        URL url = new URL("http://www.google.com/m8/feeds/contacts/default/full/");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage());
        String result = "Response: " + request.getResponseCode() + " "
                + request.getResponseMessage();
        StringBuffer buffer = new StringBuffer();
        BufferedInputStream providerResponseData = new BufferedInputStream(request.getInputStream());
        java.io.InputStreamReader isr = new java.io.InputStreamReader(providerResponseData);
        BufferedReader bb = new BufferedReader(isr);
        String index = "";
        while ((index = bb.readLine()) != null) {
        	buffer.append(index);
        	System.out.println("Data returned");
        }
        
        
    	return result + buffer.toString();
    }
}
